package com.ecube.mqtt.page.listener.vehicle;

import com.ecube.mqtt.service.BBOXService;
import com.ecube.mqtt.service.SwapSendService;
import com.ecube.mqtt.utils.CommonConstant;
import com.ecube.mqtt.utils.PropertyUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BBOXSendListener implements ActionListener {
    private JComboBox env;
    private JTextField batteryIdtext;

    private JTextField vintext;
    private JTextField totalMileagetext;
    private JTextField todayMileagetext;
    private JTextField soctext;
    private JTextField sohtext;
    private JTextField lontext;
    private JTextField lattext;
    private JTextField BBOXiccIDtext;
    private JTextField TBOXiccIDtext;
    //private JFrame start;
    public BBOXSendListener(JFrame start,
                            JComboBox env,
                            JTextField batteryIdtext,
                            JTextField vintext,
                            JTextField totalMileagetext,
                            JTextField todayMileagetext,
                            JTextField soctext,
                            JTextField sohtext,
                            JTextField lontext,
                            JTextField lattext,
                            JTextField BBOXiccIDtext,
                            JTextField TBOXiccIDtext) {
        //this.start = start;
        this.env = env;
        this.batteryIdtext = batteryIdtext;
        this.vintext = vintext;
        this.totalMileagetext = totalMileagetext;
        this.todayMileagetext = todayMileagetext;
        this.soctext = soctext;
        this.sohtext = sohtext;
        this.lontext = lontext;
        this.lattext = lattext;
        this.BBOXiccIDtext = BBOXiccIDtext;
        this.TBOXiccIDtext = TBOXiccIDtext;

    }

    public void actionPerformed(ActionEvent e) {
        Dimension dim2 = new Dimension(100, 30);
        Dimension dim3 = new Dimension(300, 30);
        Map propertys = new HashMap();

        propertys.put("env",env.getSelectedItem().toString());
        propertys.put("batteryIdtext",batteryIdtext.getText());
        propertys.put("vintext",vintext.getText());
        propertys.put("BBOXiccIDtext",BBOXiccIDtext.getText());
        propertys.put("TBOXiccIDtext",TBOXiccIDtext.getText());
        try {
            PropertyUtil.setProperty(propertys, CommonConstant.VEHICLE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //生成新界面
        JFrame startres = new JFrame();
        startres.setSize(400, 200);
        startres.setTitle("发送结果");//设置窗体标题
        startres.setDefaultCloseOperation(2);
        startres.setLocationRelativeTo(null);
        startres.setFont(new Font("宋体", Font.PLAIN, 14));  //宋体，正常风格，14号字体

        FlowLayout fl = new FlowLayout(FlowLayout.CENTER,30,30);
        //实例化流式布局类的对象
        startres.setLayout(fl);

        //创建组件
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        BBOXService cemqSendService = new BBOXService();
        Boolean rateresult = false;
        rateresult = cemqSendService.sendBBOX(env.getSelectedItem().toString(),
                batteryIdtext.getText(),
                vintext.getText(),
                totalMileagetext.getText(),
                todayMileagetext.getText(),
                soctext.getText(),
                sohtext.getText(),
                lontext.getText(),
                lattext.getText(),
                BBOXiccIDtext.getText());
        //用户名和密码
        if (rateresult) {
            JLabel message = new JLabel("BBOX上报成功");
            message.setFont(new Font("宋体", Font.PLAIN, 20));  //宋体，正常风格，14号字体
            message.setPreferredSize(dim3);
            jp1.add(message);
            startres.add(jp1, BorderLayout.CENTER);

            JButton close = new JButton("确定");
            close.setFont(new Font("宋体", Font.PLAIN, 18));
            //设置按键大小
            close.setSize(dim3);
            jp2.add(close);
            startres.add(jp2, BorderLayout.SOUTH);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    startres.dispose();
                }
            });

            startres.setResizable(false);
            startres.setVisible(true);

        } else {
            JLabel message = new JLabel("BBOX上报失败");
            message.setFont(new Font("宋体", Font.PLAIN, 20));  //宋体，正常风格，14号字体
            message.setPreferredSize(dim3);
            //将textName标签添加到窗体上
            jp1.add(message);
            startres.add(jp1, BorderLayout.CENTER);

            JButton close = new JButton("确定");
            close.setFont(new Font("宋体", Font.PLAIN, 18));
            //设置按键大小
            close.setSize(dim3);
            jp2.add(close);
            startres.add(jp2, BorderLayout.SOUTH);

            close.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    startres.dispose();
                }
            });

            startres.setResizable(false);
            startres.setVisible(true);

        }
    }
}